<?php


namespace App\Http\Controllers\WebApi;


use App\Models\ImCommunicationLogs;
use Illuminate\Http\Request;

class CommunicationController
{
    /**
     * 音视频记录
     * @param Request $req
     * @return \Illuminate\Http\JsonResponse
     */
    public function audioList(Request $req)
    {
        $initiator = $req->get('initiator');
        $recipient = $req->get('recipient');
        $type = $req->get('type');
        $status = $req->get('status');
        list($offset, $limit, $page, $pagesize) = ImCommunicationLogs::pageProcess();
        $query = ImCommunicationLogs::with(['initiator']);
        $query->whereHas('initiator');
        if (!empty($initiator)) {
            $query->whereHas('initiator', function ($sql) use ($initiator) {
                $sql->where('nickname', $initiator);
                $sql->orWhere('phone', $initiator);
            });
        }
        if (!empty($recipient)) {
            $query->whereRaw('JSON_CONTAINS(recipient, ?)', [$recipient]);
        }
        if ($type > 0) {
            $query->where('type', $type);
        }
        if ($status > 0) {
            $query->where('status', $status);
        }
        $total = $query->count();
        $data = $query->offset($offset)->limit($limit)->orderBy('id', 'desc')->get();
        $data = ImCommunicationLogs::dataProcess($data);
        return okBack($data, [
            'total' => $total,
            'page' => $page,
            'pagesize' => $pagesize,
        ]);
    }
}
